﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.IO;
using System.Drawing;

using System.Net;


namespace FilStartUps.Account
{
    public partial class EditProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    LinkButton lkHome = (LinkButton)FilStartUps_PageNav1.FindControl("lbLgout");
                    LinkButton lkProfile = (LinkButton)FilStartUps_PageNav1.FindControl("lbProfile");
                    LinkButton lklogin = (LinkButton)FilStartUps_PageNav1.FindControl("lblogin");
                    LinkButton lkregister = (LinkButton)FilStartUps_PageNav1.FindControl("lbRegister");
                    lklogin.Visible = false;
                    lkregister.Visible = false;
                    lkProfile.Visible = true;
                    lkHome.Visible = true;
                }

                int id = Convert.ToInt32(Request.QueryString["id"].Trim());
                GetEditProfile(id);
            }
        }

        private void GetEditProfile(int profileid)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var profile = db_connect.Profiles
                                .Where(p => p.profileidPK.Equals(profileid))
                                 .SingleOrDefault();

                txtBirthDate.Text = profile.birthdate.ToShortDateString();
                imgprofile.ImageUrl = "../FilStartUps.ashx?id=" + profile.profileidPK + "&cat=" + 0;
                lbprofileid.Text = profile.profileidPK.ToString();
                txtAnI.Text = profile.activitiesAndinterest;
                txtfname.Text = profile.firstname;
                txtlname.Text = profile.lastname;
                txtphonenumber.Text = profile.phonenumber;
                ddlist.SelectedValue = profile.gender;
                txtcitizenship.Text = profile.citizenship;
                txtcompanyaddress.Text = profile.companyaddress;
                txtcompanydescription.Text = profile.companydescription;
                txtcompanyname.Text = profile.companyname;
                txtcompanywebsite.Text = profile.companywebsite;

            }
        }

        protected void btsubmit_Click(object sender, EventArgs e)
        {

            if (fileuploadprofile.PostedFile.ContentType == "application/octet-stream" || fileuploadprofile.PostedFile.ContentType == "image/png" || fileuploadprofile.PostedFile.ContentType == "image/jpeg" || fileuploadprofile.PostedFile.ContentType == "png")
            {
                using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
                {
                    //save image;
                    //Temp Value(2)
                    int profid = Convert.ToInt32(lbprofileid.Text);
                    var profile = db_connect.Profiles.Where(p => p.profileidPK.Equals(profid)).SingleOrDefault();

                    //Save Images
                    if (fileuploadprofile.HasFile != false)
                    {
                        int lenght = fileuploadprofile.PostedFile.ContentLength;
                        byte[] buffer = new byte[lenght];
                        fileuploadprofile.PostedFile.InputStream.Read(buffer, 0, lenght);
                        profile.image = buffer;
                        profile.imagetype = fileuploadprofile.PostedFile.ContentType;
                        profile.filename = fileuploadprofile.PostedFile.FileName;
                    }
                    
                    profile.phonenumber = txtphonenumber.Text;
                    profile.firstname = txtfname.Text;
                    profile.lastname = txtlname.Text;
                    profile.birthdate = Convert.ToDateTime(txtBirthDate.Text);
                    profile.gender = ddlist.SelectedValue;
                    profile.citizenship = txtcitizenship.Text;
                    profile.activitiesAndinterest = txtAnI.Text;
                   
                    db_connect.Entry(profile).State = System.Data.EntityState.Modified;
                    db_connect.SaveChanges();

                }

                //Redirect MemberProfile
                //Check Session name or cookie
                Response.Redirect("../Account/MemberProfile.aspx");
            }
            else
            {
                errormsg.Visible = true;
            }
        }

        //Convert To Image
        public System.Drawing.Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            return image;
        }

        //Convert To Base64string
        public string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //convert image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                //Convert byte[] to base64string
                string base64string = Convert.ToBase64String(imageBytes);
                return base64string;
            }
        }

        protected void btneditcompany_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                int profid = Convert.ToInt32(lbprofileid.Text);
                var profile = db_connect.Profiles.Where(p => p.profileidPK.Equals(profid)).SingleOrDefault();

                profile.companyname = txtcompanyname.Text;
                profile.companydescription = txtcompanydescription.Text;
                profile.companywebsite = txtcompanywebsite.Text;
                profile.companyaddress = txtcompanyaddress.Text;

                db_connect.Entry(profile).State = System.Data.EntityState.Modified;
                db_connect.SaveChanges();
                Response.Redirect("../Account/MemberProfile.aspx");
            }
        }

    }
}